/*
 * @Description: 层级组件reducer
 * @Author: Rfan
 * @Date: 2022-06-21 10:25:48
 * @LastEditTime: 2022-06-21 20:21:15
 */

import {
  INIT_INFO,
  SET_CHANGE_METHOD,
  SET_COMPUTE_HEIGHT_SWITCH,
  SET_LIST_HEIGHT,
  SET_SELECTED_ITEM
} from '../constants';

export const LevelTableReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 表格选中的项目
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 初始化接收的坐标及上级条目信息
     */
    case INIT_INFO: {
      const { data, upper } = action;

      const baseInfo = data.map((item, idx) => {
        return item.level === 0
          ? {
              ...item,
              exportData: { ...item.exportData, id: idx + 1, title: item.exportData.file_name }
            }
          : item;
      });
      const upperFields = Object.entries(upper?.exportData || {}).map(([key]) => ({
        field_desc: key,
        field_name: key
      }));
      return { ...state, baseInfo, upperInfo: upper?.exportData, upperFields };
    }

    /**
     * @description: 设置是否计算高度开关
     */
    case SET_COMPUTE_HEIGHT_SWITCH: {
      return { ...state, isComputeHeight: action.data };
    }

    /**
     * @description: 设置表格高度
     */
    case SET_LIST_HEIGHT: {
      return { ...state, tableHeight: action.data };
    }

    /**
     * @description: 初始化接收的坐标及上级条目信息
     */
    case SET_CHANGE_METHOD:
      return { ...state, selectChange: action.data };

    default:
      console.log('untouched', action);
      return state;
  }
};
